var config = require('../../config.js');
//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    isIpx: app.globalData.isIpx,
    lawSelect1: false,
    lawSelect2: false,
    phone: app.globalData.phone,
    hero: 0,
    channel: app.globalData.channel,
    rule: false,
  },
  //事件处理函数
  bindViewTap: function () {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function (options) {

    //是否有玩游戏的资格 是传小票还是玩游戏还是弹窗
    if (options.openid) {
      // wx.showModal({
      //   title: '提示',
      //   content:options.openid,
      //   success: function(res) {

      //   }
      // })
    }

    // var phone = wx.getStorageSync('phone')

    if (app.globalData.userInfo) {

      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else {

      app.getUserInfo(function (userInfo) {

        //更新数据
        that.setData({
          userInfo: userInfo,
          hasUserInfo: true
        })
      })
    }



  }, changeLaw1: function () {
    var that = this;
    that.setData({ lawSelect1: !that.data.lawSelect1 });
  }, changeLaw2: function () {
    var that = this;
    that.setData({ lawSelect2: !that.data.lawSelect2 });
  }, showlaw: function () {
    var that = this;
    that.setData({ lawSelect1: true });

    wx.navigateTo({
      url: '/pages/law1/law1?law=1',
    });
  }, showlaw2: function () {
    var that = this;
    that.setData({ lawSelect2: !that.data.lawSelect2 });
  }, toUpTicket: function (e) {
    var that = this;
    

    if (!that.data.lawSelect1) {
      wx.showModal({
        title: '提示',
        content: '请阅读并接受《联合利华隐私保护准则》',
        success: function (res) {
          if (res.confirm) {
          } else if (res.cancel) {
          }
        }
      })
      return false;
    }

    if (!that.data.lawSelect2) {
      wx.showModal({
        title: '提示',
        content: '请勾选接受来自联合利华的沟通信息',
        success: function (res) {
          if (res.confirm) {
          } else if (res.cancel) {
          }
        }
      })
      return false;
    }




    console.log(app.globalData.avenue);
    if (app.globalData.avenue == '1001') {
      wx.navigateTo({
        url: '/pages/law1/law1?law=4',
      });
    } else if (app.globalData.avenue == '2001') {

      that.toPlayGames();//去玩游戏

    } else if (app.globalData.avenue == '2002') {
      wx.showModal({
        title: '提示',
        content: '今天的机会已用完，请明天再来',
        success: function (res) {
          if (res.confirm) {
          } else if (res.cancel) {
          }
        }
      })
    }
  },
  getPhoneNumber: function (e) {
    var that = this;
    if (e.detail.errMsg == "getPhoneNumber:ok") {
      var weappuserInfo = app.globalData.weappuserInfo;
      console.log(weappuserInfo);
      var iv = e.detail.iv;
      var encrypted_data = e.detail.encryptedData;

      wx.login({
        success: function (res) {

          var data = {
            "jsCode": res.code,
            "channelId": '5b0b6ff415a54671d5b61615',
            'iv': iv,
            'encryptedData': encrypted_data,
          };
          // document.cookie="accesstoken="+weappuserInfo.accessToken;
          var header;
          header = {
            'content-type': 'application/json',
            'cookie': "accesstoken=" + weappuserInfo.accessToken,//读取cookie
          };

          app.globalData.phone="13122908082";
          that.setData({
                  phone: app.globalData.phone,
                })
          // wx.request({
          //   url: "https://staging-ajax.quncrm.com/api/wechat/decrypt-mini-program",
          //   data: data,
          //   method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
          //   header: header, // 设置请求的 header
          //   success: function (res) {
          //     console.log(res);
          //     app.globalData.phone = res.data.phoneNumber;
          //     that.setData({
          //       phone: res.data.phoneNumber,
          //     })

          //   },
          //   fail: function (err) {
          //     console.log(err);
          //   }
          // })

        }
      })


      // 1.加载活动首页 事件
      var trackingdata = {
        "accountId": "57bd51aa5b5529760f8b4567",
        "memberId": "5575548613747370538b4571",
        "msgType": "customerEvent",
        "subType": "c_Mobile_Authenticity",
        "createTime": 1529647042000,
        "eventProperties": {
            "Authenticity_Status": "success",
            "Campaign_ID": "dts_campaign_clear_2018_marvel"
        }
      }
     app.addTracking(trackingdata);


    } else if (e.detail.errMsg == "getPhoneNumber:fail user deny") {
        // 1.加载活动首页 事件
        var trackingdata = {
          "accountId": "57bd51aa5b5529760f8b4567",
          "memberId": "5575548613747370538b4571",
          "msgType": "customerEvent",
          "subType": "c_Mobile_Authenticity",
          "createTime": 1529647042000,
          "eventProperties": {
              "Authenticity_Status": "fail",
              "Campaign_ID": "dts_campaign_clear_2018_marvel"
          }
        }
       app.addTracking(trackingdata);
    }





  }, gorecord: function () {//参与记录
    var that = this;

    wx.navigateTo({
      url: '/pages/law1/law1?law=5',
    });
  }, toPlayGames: function () {
    var that = this;
    console.log(app.globalData.channel);
    wx.navigateTo({
      url: '/pages/law1/law1?law=3',
    });
  }, showRule: function () {
    console.log('11111');
    this.setData({
      rule: true,
    })
  }, hideRule: function () {
    this.setData({
      rule: false,
    })
  }, onShareAppMessage: function (res) {
    var weappuserInfo = app.globalData.weappuserInfo;
    console.log(weappuserInfo);
    console.log(app.globalData.hero);
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: '超级英雄跳一跳，清扬福利大放送！',
      path: 'pages/index/index?openid=' + weappuserInfo.openId,
      imageUrl: '../images/' + app.globalData.hero + '.jpg',
    }
  }
})
